package com.xh.one2one.lesson.controller;

import com.google.common.collect.ImmutableMap;
import com.xh.core.annotation.ExcludeLoginTokenIntercept;
import com.xh.core.util.StringUtils;
import com.xh.one2one.lesson.bean.request.LessonTimesAuditRequest;
import com.xh.one2one.lesson.service.ILessonAuditService;
import com.xh.one2one.lesson.model.LessonAudit;
import com.xh.one2one.lesson.bean.request.LessonAuditRequest;
import com.xh.core.bean.XhExportHeaderBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import org.springframework.validation.annotation.Validated;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;

import org.springframework.web.bind.annotation.RestController;
import com.xh.core.controller.BaseController;

/**
 * <p>
 * 备课审核 前端控制器
 * </p>
 *
 * @author TW
 * @since 2021-04-22
 */
@RestController
@RequestMapping("/admin/lessonAudit")
public class LessonAuditController extends BaseController {

    @Autowired
    private ILessonAuditService lessonAuditService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryPage")
    public AjaxResult queryPage(XhPage page, LessonAuditRequest bean){
        page=lessonAuditService.queryLessonAuditPage(page,bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",page);
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/query")
    public AjaxResult query(LessonAuditRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",lessonAuditService.queryLessonAudit(bean));
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryOne")
    public AjaxResult queryOne(LessonAuditRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",lessonAuditService.queryOne(bean));
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryDetails")
    public AjaxResult queryDetails(LessonAuditRequest bean){

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",lessonAuditService.queryDetails(bean));
    }
    /**
     * 导出全部
     * @param bean
     * @return
     */
    @ExcludeLoginTokenIntercept
    @RequestMapping(value="/exportAll")
    public AjaxResult exportAll(LessonAuditRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            exportHeaderBeans.put("creatorName", new XhExportHeaderBean("教师"));
            exportHeaderBeans.put("title", new XhExportHeaderBean("课次名称"));
            exportHeaderBeans.put("lessonName", new XhExportHeaderBean("学生/小组课"));
            exportHeaderBeans.put("modifyTime", new XhExportHeaderBean("提交日期","yyyy-MM-dd HH:mm:ss"));
            exportHeaderBeans.put("status",new XhExportHeaderBean("审核状态", ImmutableMap.of(0,"未审核",1,"已通过", 2,"已拒绝")));
            String downloadUrl=lessonAuditService.exportAll(request,exportHeaderBeans,bean,"queryLessonAuditPage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     * @param lessonAudit 实体对象
     * @return
     */
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated LessonAudit lessonAudit){
        if(lessonAudit.getId()==null){
            lessonAuditService.insert(lessonAudit);
        }else{
            lessonAuditService.update(lessonAudit);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 新增
     * @param request 实体对象
     * @return
     */
    @PostMapping(value="/save")
    public AjaxResult save(@Validated @RequestBody LessonAuditRequest request){

        try {
            lessonAuditService.save(request);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"系统错误！");
        }

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        lessonAuditService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        lessonAuditService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }
}

